package com.th.cash.th2.product

import android.content.Context
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.lxj.xpopup.core.BottomPopupView
import com.th.resmodel.R
import com.th.resmodel.databinding.PopupSingleSelectionBinding
import com.th.basemodel.ext.bindData
import com.th.basemodel.ext.divider
import com.th.basemodel.ext.dp2px
import com.th.basemodel.ext.itemClick
import com.th.basemodel.ext.vertical

class SingleSelectionPopup(
    context: Context,
    private val title: String,
    val data: List<SelectItem>
) : BottomPopupView(context) {

    var clickListener: ((Int) -> Unit)? = null

    override fun getImplLayoutId(): Int {
        return R.layout.popup_single_selection
    }

    override fun onCreate() {
        super.onCreate()

        val bind = PopupSingleSelectionBinding.bind(popupImplView)

        bind.run {
            singleSelectionTitle.text = title

            singleList.vertical()
                .divider(color = ContextCompat.getColor(context, R.color.c_D8F5FC), size = dp2px(8f))
                .bindData(data, R.layout.item_single_selection) { holder, t, position ->
                    holder.itemView.findViewById<TextView>(R.id.title).text = data[position].title

                    holder.itemView.findViewById<ImageView>(R.id.isSelect).setImageResource(
                        if (data[position].isSelect) {
                            R.mipmap.img_34
                        }else{
                            R.mipmap.img_35
                        }
                    )
                }
                .itemClick<String> { data, holder, position ->
                    dismiss()
                    clickListener?.invoke(position)
                }
        }
    }

    data class SelectItem(val title:String,val id:String,val isSelect:Boolean)

}